<template>
  <div class="app-container">
    <div>
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" clearable :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.categoryId !== 0">
            <el-form-item label="分类" prop="categoryId">
              <treeselect v-model="form.categoryId" :options="categoryList" :normalizer="normalizer" placeholder="选择上级类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="封面" prop="coverImgUrl" required>
<!--              <image-upload :limit="1" ref="coverImg"  @input="setCoverImgUrl"></image-upload>-->
              <my-upload ref="coverImg" v-model="form.coverImgUrl"  @input="setCoverImgUrl"></my-upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="简介" prop="introduction">
              <el-input v-model="form.introduction" type="textarea" placeholder="请输入简介"
                        :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否热门" prop="isHot" required>
              <el-switch v-model="form.isHot"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="作者" prop="createBy">
              <el-input v-model="form.createBy" placeholder="请输入作者" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="博客内容" prop="content">
              <editor-md ref="editorMd"></editor-md>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="myReset">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { listCategory,listBlog, getBlog, delBlog, addBlog, updateBlog,getContent } from "@/api/blog/blog";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import editorMD from "../../../components/EditorMd/editorMD";
import EditorMd from "../../../components/EditorMd/editorMD";
import myUpload from "@/components/myUpload/singleUpload"

export default {
  name: "editor",
  dicts: ['sys_show_hide','sys_normal_disable'],
  components: {EditorMd, Treeselect,editorMD,myUpload},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 博客表格数据
      blogList: [],
      //分类
      categoryList: [],
      //editormd内容
      editorContent: "",
      //表单参数
      form: {
        title: undefined,
        categoryId: undefined,
        coverImgUrl: null,
        introduction: undefined,
        isHot: false,
        status: undefined,
        createBy: undefined,
        content: "",
        contentWrite: ""
      },
      //表单校验
      rules: {
        title: [{
          required: true,
          message: '请输入标题',
          trigger: 'blur'
        }],
        categoryId: [{
          required: true,
          message: '请选择分类',
          trigger: 'change'
        }],
        introduction: [{
          required: true,
          message: '请输入简介',
          trigger: 'blur'
        }],
        status: [{
          required: true,
          message: '是否可见不能为空',
          trigger: 'change'
        }],
        createBy: [{
          required: true,
          message: '请输入作者',
          trigger: 'blur'
        }],
        content: [{
          required: false,
          message: '请输入多行文本',
          trigger: 'blur'
        }],
      },
      coverImgUrlAction: process.env.VUE_APP_BASE_API + "/common/upload",
      coverImgUrlfileList: [],
      instance: null,
    };
  },
  created() {
    this.getCategories();
  },
  mounted() {
    // 将分享方法绑定在window上
    window['setEditorContent'] = () => {
      this.setEditorContent();
    }
  },
  methods: {
    /** 查询分类列表 */
    getCategories() {
      listCategory().then(response => {
        this.categoryList = this.handleTree(response.rows, "categoryId");
        this.loading = false;
      });
    },
    /** 转换类型数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.categoryId,
        label: node.text,
        children: node.children
      };
    },
    // 表单重置
    reset() {
      this.form = {
        blogId: null,
        authorId: null,
        title: null,
        content: "",
        contentWrite: "",
        categoryId: null,
        introduction: null,
        coverImgUrl: null,
        status: "0",
        delFlag: null,
        isHot: null,
        createBy: null,
        likeCount: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 重新设置 */
    myReset() {
      this.reset();
      this.open = true;
      this.$refs.editorMd.editorSetContent(this.editorContent);
      // this.$refs.coverImg.setFileList(this.form.coverImgUrl);
      this.title = "添加博客";
    },
    /** 提交按钮 */
    submitForm() {
      this.form.content = this.$refs.editorMd.getEditorHtml();
      this.form.contentWrite = this.$refs.editorMd.getEditorContent();
      this.$refs["form"].validate(valid => {
        if (valid) {
          addBlog(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.$router.push({ path: "/home/blog/blog" });
          });
        }
      });
    },
    setCoverImgUrl(arg){
      this.form.coverImgUrl = arg;
    },
    setEditorContent(){
      this.$refs.editorMd.editorSetContent(this.editorContent);
      // this.$refs.coverImg.setFileList(this.form.coverImgUrl);
      this.setCoverImgUrl(this.form.coverImgUrl);
    },
  }
}
</script>

<style scoped>
.dialog-footer{
  margin-left: 50px;
}
</style>
